<template>
  <div class="wrapper">
    <img
      class="wrapper__img"
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
    />
    <div class="wrapper__ipt">
      <input
        type="text"
        class="wrapper__ipt__content"
        placeholder="请输入用户名"
        v-model="data.username"
      />
    </div>
    <div class="wrapper__ipt">
      <input
        type="text"
        class="wrapper__ipt__content"
        placeholder="请输入密码"
        v-model="data.password"
      />
    </div>
    <div class="wrapper__ipt">
      <input
        type="text"
        class="wrapper__ipt__content"
        placeholder="确认密码"
        v-model="data.ensurePassword"
      />
    </div>
    <div class="wrapper__register-button" @click="handleRegister">注册</div>
    <div class="wrapper__register-link" @click="handleLoginClick">
      已有账号去登陆
    </div>
    <Toast v-if="toastData.showToast" :msg="toastData.toastMsg" />
  </div>
</template>
<script>
import { useRouter } from "vue-router";
import { reactive } from "vue";
import { post } from "../../utils/request";
import Toast, { useToastEffect } from "../../components/Toast";
import { useLocalDataEffect } from "../../localData/localData";
// // 将注册的用户信息追加到sessionStorage中
// const pushDataToLocal = (iptUserName, iptPwd) => {
//   const localData = JSON.parse(sessionStorage.localData || "{}");
//   localData.push({ userName: iptUserName, password: iptPwd });
//   sessionStorage.localData = JSON.stringify(localData);
// };
const useRegisterEffect = (showToast) => {
  // const router = useRouter();
  const data = reactive({ username: "", password: "", ensurePassword: "" });
  const handleRegister = async () => {
    try {
      const result = await post("/api/user/register", {
        username: data.username,
        password: data.password,
      });
      const { pushDataToLocal } = useLocalDataEffect(
        data.username,
        data.password
      );
      if (result?.data?.errno === 0) {
        if (data.password === data.ensurePassword) {
          // localStorage.isLogin = true;
          // router.push({ name: "Home" });
          pushDataToLocal(data.username, data.password);
          showToast("注册成功，返回登录");
        } else {
          showToast("两次密码输入不相同");
        }
      } else {
        showToast("注册失败");
      }
    } catch (e) {
      showToast("请求失败");
    }
  };
  return { data, handleRegister };
};

const useLoginEffcet = () => {
  const router = useRouter();
  const handleLoginClick = () => {
    router.push({ name: "Login" });
  };
  return { handleLoginClick };
};

export default {
  name: "Register",
  components: { Toast },
  setup() {
    const { toastData, showToast } = useToastEffect();
    const { data, handleRegister } = useRegisterEffect(showToast);
    const { handleLoginClick } = useLoginEffcet();
    return { toastData, showToast, data, handleRegister, handleLoginClick };
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  .wrapper__img {
    display: block;
    width: 0.66rem;
    height: 0.66rem;
    margin: 0 auto 0.4rem auto;
  }
  .wrapper__ipt {
    height: 0.48rem;
    margin: 0 0.4rem 0.2rem 0.4rem;
    .wrapper__ipt__content {
      box-sizing: border-box;
      padding: 0 0.2rem 0 0.2rem;
      width: 100%;
      height: 100%;
      font-size: 0.16rem;
      background-color: #f9f9f9;
      border: 0.01rem solid rgba(0, 0, 0, 0.1);
      border-radius: 0.06rem;
      outline: none;
      &::placeholder {
        color: #777;
      }
    }
  }
  .wrapper__register-button {
    height: 0.48rem;
    margin: 0.32rem 0.4rem 0.2rem 0.4rem;
    text-align: center;
    line-height: 0.48rem;
    font-size: 0.16rem;
    color: #fff;
    background-color: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: 0.06rem;
  }
  .wrapper__register-link {
    text-align: center;
    font-size: 0.14rem;
    line-height: 0.2rem;
    color: #777;
  }
}
</style>
